package com.njit.farm.service.impl;

import com.njit.farm.dao.FGoodsMapper;
import com.njit.farm.dataobject.FGoods;
import com.njit.farm.model.Goods;
import com.njit.farm.model.Result;
import com.njit.farm.service.GoodsService;
import com.njit.farm.util.IDUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Service
public class GoodsServiceImpl implements GoodsService {

    @Resource
    FGoodsMapper goodsMapper;

    @Override
    public Result getAll() {

        Result result = new Result();

        List<FGoods> fGoods = goodsMapper.selectAll();
        List<Goods> goodsList = new ArrayList<>();
        for (FGoods fGoods1 : fGoods) {
            goodsList.add(fGoods1.toModel());
        }
        result.setSucc(true);
        result.setData(goodsList);

        return result;
    }

    @Override
    public Result remove(String gid) {

        Result result = new Result();

        goodsMapper.deleteByPrimaryKey(gid);

        result.setSucc(true);

        return result;
    }

    @Override
    public Result add(Goods goods) {

        Result result = new Result();

        FGoods fGoods = new FGoods();
        fGoods.setgId(IDUtils.uuid());
        fGoods.setgTitle(goods.getTitle());
        fGoods.setgPrice(goods.getPrice());
        fGoods.setgSales(0);
        fGoods.setgFare(goods.getFare());
        fGoods.setgPosition(goods.getPosition());
        fGoods.setgTel(goods.getTel());
        fGoods.setgCover(goods.getCover());
        fGoods.setgContent(goods.getContent());
        fGoods.setuId(goods.getuId());

        goodsMapper.insert(fGoods);

        result.setSucc(true);
        return result;
    }


    @Override
    public Result queryByUserId(String uid) {

        Result result = new Result();

        List<FGoods> fGoods = goodsMapper.selectByUserId(uid);

        List<Goods> goodsList = new ArrayList<>();

        for (FGoods fGoods1 : fGoods) {
            goodsList.add(fGoods1.toModel());
        }

        result.setSucc(true);
        result.setData(goodsList);
        return result;
    }

    @Override
    public Result getGoods(String gid) {

        Result result = new Result();

        FGoods fGoods = goodsMapper.selectByPrimaryKey(gid);

        result.setSucc(true);
        result.setData(fGoods.toModel());

        return result;
    }
}
